<?php

/**
 * Description of MantUsuario
 *
 * @author EaDeveloper
 */
class MantCatalogs extends FastMaintenance {

    public function __construct() {
        $this->table = "cllg_util_catalog";
        $this->gridCols = array("ID" , "NOMBRE");
        $this->fieldsConfig = array("ID" => "CODIGO", "NOMBRE" => "NOMBRE");
        $this->autoPK = true;
        $this->pkFields = array("ID");
        $this->tittle = "Mantenimiento de Catalogos";
        $this->windowTittle = $this->tittle;
        $this->instanceName = "MantCatalogs";
    }

    public function includeWidget() {
        $op = array();
        if (isset($this->currentRecord[$this->pkFields[0]])) {
            $pkCat = sqlValue($this->currentRecord[$this->pkFields[0]],"int");
            $op = $this->db->queryToArray("select * from cllg_util_catalog_value where FK_CATALOG=$pkCat", true);
        }
?>
        <style type="text/css">
            #widgetopciones label{
                width:auto !important;
            }
        </style>
        <div id="widgetopciones" style="border: 1px; width: 600px; " >
            <fieldset>
                <legend>Opciones Catalogo</legend>
                <div style="float: left; width: 100%; margin-bottom: 1px; text-align: left;"><label for="coditem"> Item Valor </label><input type="text" id="coditem" />  </div>
                <div style="float: left; width: 100%; margin-bottom: 1px; text-align: left;"><label for="desitem"> Item Etiqueta </label><input type="text" id="desitem" /> </div>
                <div style="float: left; width: 100%; margin-bottom: 1px; text-align: left;"><button value="Agregar" onclick="addRow()" type="button" >Agregar</button> </div>
                <div class="dttb" style="float: left; width: 100%; margin-bottom: 1px;">
                    <table id="opciones" width="500px">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Valor</th>
                                <th>Etiqueta</th>
                                <th>Opciones</th>
                            </tr>
                        </thead>
                        <tbody>
                    <?php foreach ($op as $c) {
                    ?>
                        <tr id="row<?php echo $c["ID"]; ?>">
                            <td><?php echo $c["ID"]; ?></td>
                            <td><?php echo $c["VALUE"]; ?></td>
                            <td><?php echo $c["LABEL"]; ?></td>
                            <td><button type="button" onclick="javascript:removeRow('<?php echo $c["ID"]; ?>');"> Quitar </button> </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
            <script type="text/javascript">
                $(":button").button();
                function removeRow(r){
                    $("#row"+r).remove();
                    $("#addopcioncod-"+r).remove();
                    $("#addopciondes-"+r).remove();
                    $("#widgetopciones").append("<input type='hidden' value='"+r+"' name='delopcion-"+r+"' />");
                }
                function addRow(){
                        $('#opciones tr:last').after('<tr id="row'+$("#coditem").val()+'"><td>'+0+'</td><td>'+
                        $("#coditem").val()+'</td><td>'+$("#desitem").val()
                        +'</td><td><button type="button" onclick="javascript:removeRow('+$("#coditem").val()+');"> Quitar </button> </td></tr>');
                         $("#widgetopciones").append("<input type='hidden' value='"+$("#coditem").val()+"-"+$("#desitem").val()+"' name='addopcion-"+$("#coditem").val()+"' />");
                         $(":button").button();
                         $("#coditem").val("");
                         $("#desitem").val("");
                }
				addFunc(function(){
					$("#opciones tbody tr").remove();
				});
            </script>
        </div>
    </fieldset>
</div>
<?php
                }

                public function delRec() {
                    parent::delRec();

                }

                public function saveRec() {
                    parent::saveRec();
                    
                }

                public function updateRec() {
                    parent::updateRec();
                    $pk=getParam($this->pkFields[0]);
                    reset($_REQUEST);
                    while ($arrGET = each($_REQUEST)) {
                        $arrExplode = explode("-", $arrGET["key"]);
                        if (isset($arrExplode[1])) {
                            $tipo=$arrExplode[0];
                            if($tipo=="delopcion"){
                                $this->db->query_delete('cllg_util_catalog_value',
                                        sprintf("ID=%s",
                                                sqlValue($arrGET["value"], 'text')));
                            }
                            if($tipo=="addopcion"){
                                $valores=explode("-",$arrGET["value"]);
                                $this->db->query_insert('cllg_util_catalog_value',array("VALUE"=>sqlValue($valores[0],"text"),
                                    "LABEL"=>sqlValue($valores[1],"text"),"FK_CATALOG"=>$pk));
                            }
                        }
                    }
                }

            }
?>
